package mo.leetcode_top;


import java.util.Stack;

public class L20 {

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray()) {
            if (c == '(' || c == '[' || c == '{') {
                stack.push(c);
            } else {
                if (stack.isEmpty()) {
                    return false;
                }
                char head = stack.pop();
                if (c == ')' && head != '(') {
                    return false;
                }
                if (c == ']' && head != '[') {
                    return false;
                }
                if (c == '}' && head != '{') {
                    return false;
                }
            }
        }
        return stack.size() == 0;
    }

    public static void main(String[] args) {
        L20 test = new L20();
        System.out.println(test.isValid("{[]}"));
    }
}
